package com.ibm.datamodel.exp;

import java.util.Arrays;

public class SchemaReader {

	LongSchema schema;
	
	public SchemaReader(LongSchema schema) {
		this.schema = schema;
		if (schema.isFix) {
			this.schema.constructn2index();
		}
	}
	
	public Comparable getValue(String name,CompareMessage message){
		int index = -1;
		if (schema.isFix) {
			index = this.schema.name2index.get(name);
		}else{
			this.schema.constructn2index(message);
			index = this.schema.name2index.get(name);
		}
		return message.getValues()[index];
	}
	
	public Comparable[] getArrayValue1(String name,CompareMessage message){
		Comparable[] value;
		int index = -1;
		if (schema.isFix) {
			index = schema.name2index.get(name);
			int offset = schema.fieldsMap.get(name).getDimension();
			value = new Comparable[offset];
//			for (int i = 0; i < offset; i++) {
//				value[i] = message.getValues()[index + i];
//			}
//			value = Arrays.copyOfRange(message.values, index, index + offset); 
			System.arraycopy(message.getValues(), index, value, 0, offset);
		}else{
			schema.constructn2index(message);
			index = schema.name2index.get(name);
			int offset = schema.fieldsMap.get(name).getDimension();
			value = new Comparable[offset];
			for (int i = 0; i < offset; i++) {
				value[i] = message.getValues()[index + i];
			}
		}
		return value;
	}
	public Comparable[] getArrayValue2(String name,CompareMessage message){
		Comparable[] value;
		int index = -1;
		if (schema.isFix) {
			index = schema.name2index.get(name);
			int offset = schema.fieldsMap.get(name).getDimension();
			value = new Comparable[offset];
//			for (int i = 0; i < offset; i++) {
//				value[i] = message.getValues()[index + i];
//			}
			value = Arrays.copyOfRange(message.values, index, index + offset); 
//			System.arraycopy(message.getValues(), index, value, 0, offset);
		}else{
			schema.constructn2index(message);
			index = schema.name2index.get(name);
			int offset = schema.fieldsMap.get(name).getDimension();
			value = new Comparable[offset];
			for (int i = 0; i < offset; i++) {
				value[i] = message.getValues()[index + i];
			}
		}
		return value;
	}
	
	public Comparable[] getArrayValue3(String name,CompareMessage message){
		Comparable[] value;
		int index = -1;
		if (schema.isFix) {
			index = schema.name2index.get(name);
			int offset = schema.fieldsMap.get(name).getDimension();
			value = new Comparable[offset];
			for (int i = 0; i < offset; i++) {
				value[i] = message.getValues()[index + i];
			}
//			value = Arrays.copyOfRange(message.values, index, index + offset); 
//			System.arraycopy(message.getValues(), index, value, 0, offset);
		}else{
			schema.constructn2index(message);
			index = schema.name2index.get(name);
			int offset = schema.fieldsMap.get(name).getDimension();
			value = new Comparable[offset];
			for (int i = 0; i < offset; i++) {
				value[i] = message.getValues()[index + i];
			}
		}
		return value;
	}
}
